package me.minetsh.imaging.crop;


import io.reactivex.Single;
import me.minetsh.imaging.crop.callback.SaveCallback;
import ohos.media.image.PixelMap;
import ohos.utils.net.Uri;

public class SaveRequest {

  private CropImageView cropImageView;
  private PixelMap image;
  private String format;
  private int compressQuality = -1;

  /**
   * 构造函数
   *
   * @param cropImageView cropImageView
   * @param image image
   */
  public SaveRequest(CropImageView cropImageView, PixelMap image) {
    this.cropImageView = cropImageView;
    this.image = image;
  }

  /**
   * compressFormat
   *
   * @param compressFormat compressFormat
   * @return this
   */
  public SaveRequest compressFormat(String compressFormat) {
    this.format = compressFormat;
    return this;
  }

  /**
   * compressQuality
   *
   * @param compressQuality compressQuality
   * @return this
   */
  public SaveRequest compressQuality(int compressQuality) {
    this.compressQuality = compressQuality;
    return this;
  }

  private void build() {
    if (format != null) {
      cropImageView.setCompressFormat(format);
    }
    if (compressQuality >= 0) {
      cropImageView.setCompressQuality(compressQuality);
    }
  }

  /**
   * execute
   *
   * @param callback callback
   */
  public void execute(SaveCallback callback) {
    build();
    cropImageView.saveAsync(image, callback);
  }

  /**
   * executeAsSingle
   *
   * @return Uri
   */
  public Single<Uri> executeAsSingle() {
    build();
    return cropImageView.saveAsSingle(image);
  }
}
